<?php

class Search extends CI_Controller {

    public function __construct()
	{
		parent::__construct();
		
	}

	//the home page of the blog
	public function index()
	{
		$data['title'] = 'Search in Portal';
		$data['message'] = '';
		$keyword = NULL;
		$categoryID = NULL;
		if ($this->input->post('keyword')){
			$keyword = $this->input->post('keyword');
		}
		if ($this->input->post('searchCatg')){
			$categoryID = $this->input->post('searchCatg');
		}
		$list['keyword'] = $keyword;
		$list['categoryID'] = $categoryID;
		$results = $this->_get_search_result($keyword, $categoryID);
		$list['noresult'] = true;

		if (count($results) > 0) {
			$list['noresult'] = false;
		}

		if (isset($results['classifieds'])) {
			if (count($results['classifieds']) > 0)
				$list['classifieds'] = $results['classifieds'];
		}
		if (isset($results['property'])){
			if (count($results['property']) > 0)
				$list['property'] = $results['property'];
		}
		if (isset($results['automobile'])){
			if (count($results['automobile']) > 0)
				$list['automobile'] = $results['automobile'];
		}

		//echo $keyword .''. $categoryID;
		
		$this->load->view('/includes/header', $data);
		$this->load->view('pages/search', $list);
		$this->load->view('/includes/footer');
	}
	
	//get search list
	function _get_search_result($keyword = NULL, $categoryID = NULL){
		//if ($keyword == NULL)
		//	return false;
		$results = array();
		if ($categoryID != NULL){
			$data = explode("_", $categoryID);
			$parentID = $data[0];
			$childID = $data[1];
			
			switch ($parentID){
				case 1:
					$this->load->model('Classified_model','',TRUE);
					$this->load->model('Classifiedscatg_model','',TRUE);
					$cond['categoryID'] = $childID;
					$this->Classifiedscatg_model->increase_count($childID);
					$results['classifieds'] = $this->Classified_model->get_search_result($keyword, $cond); break;
				case 2:
					$this->load->model('Property_model', '',TRUE);
					$cond['categoryID'] = $childID;
					$results['property'] = $this->Property_model->get_search_result($keyword, $cond); break;
				case 3:
					$this->load->model('Automobile_model', '', TRUE);
					$cond['categoryID'] = $childID;
					$results['automobile'] = $this->Automobile_model->get_search_result($keyword, $cond); break;
				default:
					break;
			}
		}else{
				$this->load->model('Classified_model','',TRUE);
				$results['classifieds'] = $this->Classified_model->get_search_result($keyword);

				$this->load->model('Property_model', '',TRUE);
				$results['property'] = $this->Property_model->get_search_result($keyword);
				
				$this->load->model('Automobile_model', '', TRUE);
				$results['automobile'] = $this->Automobile_model->get_search_result($keyword); 
		}
			return $results;
	}

}

/* End of file articles.php */
/* Location: ./controllers/admin/articles.php */ 

?>
